package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.FOrder;
import com.burgeon.backend.SHOPBACKEND.entity.UpOrder;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**根据云仓订单的单号查询云仓修改单的id*/
public class QuerryYcMretailId  extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QuerryYcMretailId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<UpOrder> getData(String docno) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(docno), url);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<UpOrder> orderArrayList = new ArrayList<UpOrder>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);

            UpOrder uorder = new UpOrder();

            uorder.setId(jsonArray.getJSONArray(i).getString(0));
            uorder.setDocno(jsonArray.getJSONArray(i).getString(1));
            uorder.setIsUp(jsonArray.getJSONArray(i).getString(2));


            orderArrayList.add(uorder);
        }
        return orderArrayList;
    }

    private MultiValueMap Data(String docno) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("DOCNO");
        columns.add("IS_UP");

        params.put("columns", columns);
        params.put("table", "YC_MRETAIL");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号
        String ac = "'="+docno+"'";
        paramss.put("column", "'DOCNO'");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QuerryYcMretailId querryYcMretailId = new QuerryYcMretailId("39.100.45.8:55322", "nea@burgeon.com.cn", "123");
        try {
            System.err.println(querryYcMretailId.getData("YCRE2004170000001"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
